/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.service;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.druid.support.json.JSONUtils;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.UserUtil;
import com.thinkgem.jeesite.modules.cloudfactory.dao.CloudfactoryUserFactoryDao;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUserFactory;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 单表生成Service
 * @author ThinkGem
 * @version 2018-09-05
 */
@Service
@Transactional
public class CloudfactoryUserFactoryService extends CrudService<CloudfactoryUserFactoryDao, CloudfactoryUserFactory> {

	public CloudfactoryUserFactory get(String id) {
		return super.get(id);
	}
	
	public List<CloudfactoryUserFactory> findList(CloudfactoryUserFactory cloudfactoryUserFactory) {
		return super.findList(cloudfactoryUserFactory);
	}
	
	public Page<CloudfactoryUserFactory> findPage(Page<CloudfactoryUserFactory> page, CloudfactoryUserFactory cloudfactoryUserFactory) {
		return super.findPage(page, cloudfactoryUserFactory);
	}
	
	@Transactional(readOnly = false)
	public void save(CloudfactoryUserFactory cloudfactoryUserFactory) {
		super.save(cloudfactoryUserFactory);
	}
	
	@Transactional(readOnly = false)
	public void delete(CloudfactoryUserFactory cloudfactoryUserFactory) {
		super.delete(cloudfactoryUserFactory);
	}
	/**
	 * 保存用户工厂
	 */
	public String saveFactory(HttpServletRequest request, Model model) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
		//request.setCharacterEncoding("utf-8");
		CloudfactoryUser user = UserUtil.getSessionUser(request);
		String companyNo = URLDecoder.decode(request.getParameter("companyNo"), "UTF-8");
		String companyName = URLDecoder.decode(request.getParameter("companyName"), "UTF-8");
		String lankman = URLDecoder.decode(request.getParameter("lankman"), "UTF-8");
		String lankWay = URLDecoder.decode(request.getParameter("lankWay"), "UTF-8");
		String mobile = URLDecoder.decode(String.valueOf(request.getParameter("mobile")), "UTF-8");
		String mail = URLDecoder.decode(request.getParameter("mail"), "UTF-8"); 
		CloudfactoryUserFactory factory = new CloudfactoryUserFactory();
		User us = new User();
		us.setId(user.getId());
		factory.setUser(us);
		factory.setContactWay(lankWay);
		factory.setFactoryName(companyName);
		factory.setFactoryNo(companyNo);
		factory.setLinkman(lankman);
		factory.setMobile(mobile);
		factory.setEmail(mail);
		super.save(factory);
		map.put("success", "success");
		return JSONUtils.toJSONString(map);
		}catch (Exception e) {
			e.printStackTrace();
			return JSONUtils.toJSONString(map.put("success","添加工厂失败"));
		}
	}
	
}